﻿
using ExaminationWeb.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ExaminationWeb
{
    public class UserAuthAttribute : AuthorizeAttribute
    {
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            if (filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true)
                || filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true))
            {
                return;
            }

            if (!UserAccount.IsLogIn())//判断是否已经登录
            {
                string fromUrl = VerifyTool.Encode(filterContext.HttpContext.Request.Url.PathAndQuery, "Wicresoft");
                string redirectUrl = string.Format("?FromUrl={0}", fromUrl);
                string loginUrl = new UrlHelper(filterContext.RequestContext).Action("Login", "Account") + redirectUrl;
                filterContext.HttpContext.Response.Redirect(loginUrl.ToLower(), true);
            }
        }
    }
}